package com.ruoyi.web.controller.byw;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.vo.PlacesVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BywPlaces;
import com.ruoyi.system.service.IBywPlacesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2024-05-29
 */
@RestController
@RequestMapping("/byw/places")
public class BywPlacesController extends BaseController
{
    @Autowired
    private IBywPlacesService bywPlacesService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('byw:places:list')")
    @GetMapping("/list")
    public TableDataInfo list(BywPlaces bywPlaces)
    {
        startPage();
        List<BywPlaces> list = bywPlacesService.selectBywPlacesList(bywPlaces);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('byw:places:list-province-city')")
    @GetMapping("/list-province-city")
    public List<PlacesVo> listProvinceCity(BywPlaces bywPlaces)
    {
        bywPlaces.setCtype(1);
        List<BywPlaces> provinceList = bywPlacesService.selectBywPlacesList(bywPlaces);
        BywPlaces query = new BywPlaces();
        query.setCtype(2);
        List<BywPlaces> cityList = bywPlacesService.selectBywPlacesList(query);

        List<PlacesVo> placesList = new ArrayList<>();
        for (BywPlaces places : provinceList){
            PlacesVo placesVo = new PlacesVo();
            placesVo.setId(places.getId());
            placesVo.setParentId(places.getParentId());
            placesVo.setCname(places.getCname());
            placesVo.setCtype(places.getCtype());

            List<PlacesVo> placesCityList = new ArrayList<>();
            for (BywPlaces city : cityList){
                if (places.getId().equals(city.getParentId())){
                    PlacesVo placesVo2 = new PlacesVo();
                    placesVo2.setId(city.getId());
                    placesVo2.setParentId(city.getParentId());
                    placesVo2.setCname(city.getCname());
                    placesVo2.setCtype(city.getCtype());
                    placesCityList.add(placesVo2);
                }
            }
            placesVo.setPlacesList(placesCityList);
            placesList.add(placesVo);
        }
        return placesList;
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('byw:places:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BywPlaces bywPlaces)
    {
        List<BywPlaces> list = bywPlacesService.selectBywPlacesList(bywPlaces);
        ExcelUtil<BywPlaces> util = new ExcelUtil<BywPlaces>(BywPlaces.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('byw:places:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(bywPlacesService.selectBywPlacesById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('byw:places:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BywPlaces bywPlaces)
    {
        return toAjax(bywPlacesService.insertBywPlaces(bywPlaces));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('byw:places:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BywPlaces bywPlaces)
    {
        return toAjax(bywPlacesService.updateBywPlaces(bywPlaces));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('byw:places:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(bywPlacesService.deleteBywPlacesByIds(ids));
    }
}
